package com.glela.api.upload.util;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class FopParse
{

	public static class Item
	{
		private String cmd;
		private String key;
		private int code;
		private boolean mp3;
		private String url;

		public String getCmd()
		{
			return cmd;
		}

		public int getCode()
		{
			return code;
		}

		public String getKey()
		{
			return key;
		}

		public String getUrl()
		{
			return url;
		}

		public boolean isMp3()
		{
			return mp3;
		}

		public void setCmd(String cmd)
		{
			this.cmd = cmd;
		}

		public void setCode(int code)
		{
			this.code = code;
		}

		public void setKey(String key)
		{
			this.key = key;
		}

		public void setMp3(boolean mp3)
		{
			this.mp3 = mp3;
		}

		public void setUrl(String url)
		{
			this.url = url;
		}
	}

	private final String httpbase;

	public FopParse(String httpbase)
	{
		this.httpbase = httpbase;
	}

	public List<Item> parse(String content)
	{

		List<Item> result = new ArrayList<Item>();

		JSONObject obj = JSON.parseObject(content);
		JSONArray items = obj.getJSONArray("items");
		if (items == null) {
			return result;
		}

		for (int i = 0; i < items.size(); i++) {

			if (!"0".equals(items.getJSONObject(i).getString("code"))) {
				continue;
			}

			Item e = new Item();
			e.setCmd(items.getJSONObject(i).getString("cmd"));
			e.setCode(items.getJSONObject(i).getInteger("code"));
			e.setKey(items.getJSONObject(i).getString("key"));
			e.setMp3(e.getCmd().indexOf("avthumb/mp4") != -1);
			e.setUrl(httpbase + e.getKey());

			result.add(e);
		}

		return result;
	}
}